﻿using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Linq;

using Timer = System.Windows.Forms.Timer;

namespace MinecraftBackupAssistant.Forms
{
    public partial class SavesUtilityForm : Form
    {
        private string savePath;
        private string fromPath;
        private string toPath;
        private string tempPath;

        private string fromWorld;
        private string toWorld;

        public Action UpdateForm;

        public SavesUtilityForm()
        {
            InitializeComponent();
        }

        private void setSwapState(bool enabled)
        {
            worldFromComboBox.Enabled = enabled;
            worldToComboBox.Enabled = enabled;

            cancelButton.Enabled = enabled;
            replaceButton.Enabled = enabled;
        }
        private void SavesUtilityForm_Load(object sender, EventArgs e)
        {
            savePath = Properties.Settings.Default.MinecraftPortablePath != "" ? Properties.Settings.Default.MinecraftPortablePath : Utility.CombinePath(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), ".minecraft", "saves");
            DirectoryInfo[] directories = new DirectoryInfo(savePath).GetDirectories();

            if (directories.Length == 0)
            {
                MessageBox.Show("Could not find any saves", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
                return;
            }

            worldFromComboBox.Items.Clear();
            worldToComboBox.Items.Clear();

            foreach (var d in directories)
            {
                worldFromComboBox.Items.Add(d.Name);
                worldToComboBox.Items.Add(d.Name);
            }

            Utility.AdjustComboBoxWidth(worldFromComboBox);
            Utility.AdjustComboBoxWidth(worldToComboBox);

            if (worldFromComboBox.Items.Count != 0)
                worldFromComboBox.SelectedIndex = 0;
            if (worldToComboBox.Items.Count != 0)
                worldToComboBox.SelectedIndex = 0;
        }

        private void replaceButton_Click(object sender, EventArgs e)
        {
            setSwapState(false);
            try
            {
                fromWorld = worldFromComboBox.Text;
                toWorld = worldToComboBox.Text;
                if (fromWorld == toWorld)
                {
                    MessageBox.Show("You can't swap a world with itself!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    setSwapState(true);
                    return;
                }

                fromPath = Path.Combine(savePath, fromWorld);
                toPath = Path.Combine(savePath, toWorld);
                tempPath = Utility.CombinePath(Utility.TemporarySwapPath, toWorld);
                Utility.PrepareDirectories();

                if (Utility.IsDirectoryEmpty(toPath))
                {
                    if (Directory.Exists(toPath))
                        Utility.DeleteDirectory(toPath);
                    Directory.Move(fromPath, toPath);
                    swapEnded();
                    return;
                }
                if (Directory.Exists(tempPath))
                    Utility.DeleteDirectory(tempPath);

                Directory.Move(toPath, tempPath);
                Directory.Move(fromPath, toPath);
                Directory.Move(tempPath, fromPath);

                swapEnded();
            }
            catch (Exception exception)
            {
                this.Invoke(new Action<Exception>(Utility.HandleError), exception);
            }
        }
        private void swapEnded()
        {
            if (UpdateForm != null)
                UpdateForm();

            string message = string.Format("{0} has been swapped with {1}.", fromWorld, toWorld);
            MessageBox.Show(message, "Saves Swapped", MessageBoxButtons.OK, MessageBoxIcon.Information);
            setSwapState(true);
            SavesUtilityForm_Load(null, null);
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SavesUtilityForm_HelpButtonClicked(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            HelpForm form = new HelpForm(HelpTab.SwapSaves);
            form.ShowDialog();
        }
    }
}
